home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group97a.txt / 000000_icon-group-sender _Thu Jan 2 11:02:16 1997.msg next >
Internet Message Format  |  2000-09-20  |  3KB

  1. Received: by cheltenham.cs.arizona.edu; Fri, 3 Jan 1997 06:35:40 MST
  2. To: icon-group@cs.arizona.edu
  3. Date: Thu, 02 Jan 1997 11:02:16 -0800
  4. From: Stuart Robinson <robinstu@ohsu.edu>
  5. Message-Id: <32CC0638.692B@ohsu.edu>
  6. Organization: Oregon Health Sciences University
  7. Sender: icon-group-request@cs.arizona.edu
  8. Reply-To: robinstu@ohsu.edu
  9. Subject: Help for an Icon Neophyte
  10. Errors-To: icon-group-errors@cs.arizona.edu
  11. Status: RO
  12. Content-Length: 2468
  13.  
  14. Hello, there.
  15.  
  16. In my last posting, I put out an APB on Corre's book, Icon Programming
  17. for Humanists, and received many helpful responses.  Thanks.  I finally
  18. managed to lay my hands on the book.
  19.  
  20. Now that I have started teaching myself Icon, I sometimes find that I
  21. reach dead-ends, and there is no one around to steer me in the right
  22. direction.  So, I thought that I would post problem programs to this
  23. newsgroup and hope that some kind soul with more knowledge of Icon would
  24. step in and lend a hand.  I hope this is appropriate.
  25.  
  26. I am trying to write a program to test Zipf's Law.  Zipf was a linguist
  27. who observed that when you take the frequency of the words in a text and
  28. rank them from most to least frequent that you can then create a log-log
  29. plot of frequency against rank and obtain roughly a straight line.
  30.  
  31. The program that I have created thus far is taken almost directly from
  32. The Icon Programming Language.  Here it is:
  33.  
  34. procedure main()
  35.  
  36. num := 0
  37.  
  38. wlist := sort(countwords(), 4)
  39.     while num +:= 1 do {
  40.     write(left(num||" "||get(wlist), 12), right(get(wlist), 4))
  41.     }
  42. end
  43.  
  44. procedure countwords()
  45.  
  46. wordcount := table(0)
  47.  
  48. while line := read() do
  49.     line ? {
  50.             while tab(upto(&letters)) do
  51.                 wordcount[tab(many(&letters))] +:= 1
  52.             }
  53.  
  54. return wordcount
  55.  
  56. end
  57.  
  58. I would like to the program to take a text, convert all of the letters
  59. to lowercase (so that "Which" and "which" are not counted as separate
  60. words), create a table of all of the words along with their associated
  61. frequencies, and to print out the results in ranked fashion.  For
  62. example, given the following text
  63.  
  64. Which is the dog which can outrace the cars?
  65.  
  66. I would like output more or less as follows (please ignore formatting--I
  67. will eventually use entab() for ease of exportation into a program such
  68. as Excel):
  69.  
  70. 1 the 2
  71. 2 which 2
  72. 3 can 1
  73. 4 cars 1
  74. 5 dog 1
  75. 6 is 1
  76. 7 outrace 1
  77.  
  78. I have run into the following problems.  First, I was unsuccessful at
  79. mapping lowercase letters on to uppercase letters.  I assume that I want
  80. to use something like map(line, &ucase, &lcase), but where should I
  81. insert it?  And should the csets &ucase and &lcase be bracketted by
  82. single quotations marks?  Second, the program seems to hang up at the
  83. end.  I have no idea why that's happening.
  84.  
  85. Eventually I would like to have the program do the log-log plot, but for
  86. now I can use the resulting output for exportation.
  87.  
  88. Thanks in advance for any help you can provide.
  89.  
  90. Stuart Robinson
  91. srobinso@reed.edu, robinstu@ohsu.edu
  92.